package com.huaixia.shorturl.controller;

import com.huaixia.shorturl.common.ApiResponse;
import com.huaixia.shorturl.domain.RequestRecord;
import com.huaixia.shorturl.domain.UrlMap;
import com.huaixia.shorturl.dto.CreateShortUrlVo;
import com.huaixia.shorturl.dto.ShortUrlQuery;
import com.huaixia.shorturl.pagehelper.PageQuery;
import com.huaixia.shorturl.pagehelper.PageResult;
import com.huaixia.shorturl.service.RequestRecordService;
import com.huaixia.shorturl.service.ShortUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author biliyu
 * @date 2023/6/6 09:39
 */
@RestController
public class ShortUrlController {

    @Autowired
    private ShortUrlService shortUrlService;

    @Autowired
    private RequestRecordService requestRecordService;

    /**
     * 创建短链接
     *
     * @param createShortUrlVo 创建短链接的请求对象
     * @return ApiResponse<String> 包含创建短链接结果的响应对象
     */
    @PostMapping("/createShortUrl")
    public ApiResponse<String> createShortUrl(@RequestBody CreateShortUrlVo createShortUrlVo) {
        return shortUrlService.createShortUrl(createShortUrlVo);
    }


    /**
     * 根据短链接的key进行重定向
     *
     * @param request HttpServletRequest对象
     * @param key     短链接的唯一标识符
     * @return RedirectView 重定向视图
     */
    @GetMapping(value = "/{key}")
    public RedirectView redirect(HttpServletRequest request, @PathVariable String key) {
        return shortUrlService.redirect(request, key);
    }

    /**
     * 获取短链接列表
     *
     * @param query 查询条件对象
     * @return ApiResponse<PageResult<UrlMap>> 包含短链接列表结果的响应对象
     */
    @PostMapping("/shortUrlList")
    public ApiResponse<PageResult<UrlMap>> shortUrlList(@RequestBody ShortUrlQuery query) {
        return shortUrlService.getShortUrlList(query);
    }


    /**
     * 获取请求记录列表
     *
     * @param query 查询条件对象
     * @return ApiResponse<PageResult < RequestRecord>> 包含请求记录列表结果的响应对象
     */
    @PostMapping("/requestRecordList")
    public ApiResponse<PageResult<RequestRecord>> requestRecordList(@RequestBody PageQuery query) {
        return requestRecordService.getRequestRecordList(query);
    }


}
